Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

147
Vistas
How to perform both a spread "operation" AND a callback function with useState in React

I am using functional components instead of class-based components. I have a state of hikes that is an array of objects and whenever I want to add a new hike to that array, I invoke the following line:

setCoors((coors=> [...coors, newCoor]))

But I also NEED to have the state callback function implemented. This is what Ive tried and it "sort of" works.

setCoors({coors:[...coors, newCoor]}, ()=>{console.log(coors)} )

But this is what I get back:

{
   coors:[
      0: {lat: 37.00, lng: -100.12},
      1: {lat: 88.00, lng: 76.12},
   ]
}

But i dont want my coor state to be an object...I need it to be an array like this:

{
   [
      0: {lat: 37.00, lng: -100.12},
      1: {lat: 88.00, lng: 76.12},
   ]
}

I can probably destructure the object to get what i want but I wanna know if there is an actual solution to this with setState. Does anyone have an idea of achieving this while also implementing a callback function? Thank you in advance

about 4 years ago · Santiago Trujillo
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda